package com.mujin.keji.collectionsystem.module.tripplan.adapter;

import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.baidu.mapapi.search.geocode.GeoCodeResult;
import com.baidu.mapapi.search.geocode.GeoCoder;
import com.baidu.mapapi.search.geocode.OnGetGeoCoderResultListener;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeOption;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeResult;
import com.baidu.mapapi.search.sug.SuggestionResult;
import com.mujin.keji.collectionsystem.R;
import com.mujin.keji.collectionsystem.listeners.CustomItemClickListener;
import com.mujin.keji.collectionsystem.utils.EmptyUtils;

import java.util.List;


/**
 * 公交路线规划列表适配器
 *
 * @author weishixiong
 * @Time 2018-04-10
 */

public class StrartAddressAdapter extends RecyclerView.Adapter<StrartAddressAdapter.ViewHolder> {
    private Activity mContext;
    private List<SuggestionResult.SuggestionInfo> datas;
    private CustomItemClickListener listener;
    GeoCoder mSearch = GeoCoder.newInstance();
    public StrartAddressAdapter(Activity context, List<SuggestionResult.SuggestionInfo> datas, CustomItemClickListener listener) {
        mContext = context;
        this.datas = datas;
        this.listener = listener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_strartaddress, parent, false);
        ViewHolder viewHolder = new ViewHolder(view, listener);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        try {
            holder.tvName.setText(datas.get(position).key);
            mSearch.setOnGetGeoCodeResultListener(new OnGetGeoCoderResultListener() {
                @Override
                public void onGetGeoCodeResult(GeoCodeResult geoCodeResult) {

                }
                @Override
                public void onGetReverseGeoCodeResult(ReverseGeoCodeResult reverseGeoCodeResult) {
                    if (EmptyUtils.isNotEmpty(reverseGeoCodeResult)) {
                        holder.tvAdress.setText(reverseGeoCodeResult.getAddress());
                    }
                }
            });
            mSearch.reverseGeoCode(new ReverseGeoCodeOption()
                    .location(datas.get(position).pt));
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    @Override
    public int getItemCount() {
        return datas == null ? 0 : datas.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView tvName;
        private TextView tvAdress;

        public ViewHolder(final View itemView, final CustomItemClickListener listener) {
            super(itemView);
            tvName = (TextView) itemView.findViewById(R.id.tv_name);
            tvAdress = (TextView) itemView.findViewById(R.id.tv_adress);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.onItemClick(itemView, getAdapterPosition());
                }
            });
        }

    }


}
